package com.newly.center.sys.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.sys.feign.LoginRecordClient;
import com.newly.center.sys.service.loginRecord.api.ILoginRecordService;
import com.newly.common.base.annotation.NewlyLog;
import com.newly.common.base.constants.MenuConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.PageUtils;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.sys.loginRecord.dto.LoginRecordDto;
import com.newly.common.base.entity.sys.loginRecord.po.LoginRecordPo;
import com.newly.common.base.entity.sys.loginRecord.vo.LoginRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author gjm guan
 * @date 2021/9/1 16:25
 * @desc
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_SYS_LOGIN_RECORD)
public class LoginRecordApi implements LoginRecordClient {

    @Autowired
    private ILoginRecordService loginRecordService;

    @NewlyLog(ability = MenuConstant.Ability.ADD, title = "登录历史", isOpen = false)
    @Override
    public R create(LoginRecordDto loginRecordDto) {
        boolean result = false;
        try {
            result = loginRecordService.save(loginRecordDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @NewlyLog(ability = MenuConstant.Ability.QUE, title = "登录历史", isOpen = false)
    @Override
    public R list(Integer offset, Integer limit, String fromTime, String toTime, String keyword) {
        PageUtils pageUtils = null;
        try {
            pageUtils = loginRecordService.list(offset, limit, fromTime, toTime, keyword);
            if (ObjectUtil.isNotEmpty(pageUtils) && ObjectUtil.isNotEmpty(pageUtils.getList())) {
                List<LoginRecordVo> loginRecordVos = BeanUtil.copyToList(pageUtils.getList(), LoginRecordVo.class);
                pageUtils.setList(loginRecordVos);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(pageUtils);
    }

    @Override
    public R queryById(Long loginRecordId) {
        LoginRecordVo recordVo = null;
        try {
            LoginRecordPo recordPo = loginRecordService.getById(loginRecordId);
            if (ObjectUtil.isNotEmpty(recordPo)) {
                recordVo = BeanUtil.copyProperties(recordPo, LoginRecordVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(recordVo);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "登录历史", isOpen = true)
    @Override
    public R deleteByIds(List<Long> loginRecordIds) {
        boolean result = false;
        try {
            result = loginRecordService.removeByIds(loginRecordIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "登录历史", isOpen = true)
    @Override
    public R clear() {
        boolean result = false;
        try {
            result = loginRecordService.clear();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }
}
